package webui

import (
	"dingbot/config"
	"dingbot/global"
	"dingbot/plugin"
	"dingbot/tools"
	"os"
	"path/filepath"

	"github.com/gofiber/fiber/v2"
)

const DefaultPluginJson string = "{}"

type LoginResultModal struct {
	Code   int `json:"code"`
	Result struct {
		Token string `json:"token"`
	} `json:"result"`
	Message string `json:"message"`
	Type    string `json:"type"`
}
type LoginErrorModal struct {
	Code    int         `json:"code"`
	Result  interface{} `json:"result"`
	Message string      `json:"message"`
	Type    string      `json:"type"`
}

type AdminInfoModal struct {
	Code   int `json:"code"`
	Result struct {
		UserID      string `json:"userId"`
		Username    string `json:"username"`
		RealName    string `json:"realName"`
		Avatar      string `json:"avatar"`
		Desc        string `json:"desc"`
		Password    string `json:"password"`
		Token       string `json:"token"`
		Permissions []struct {
			Label string `json:"label"`
			Value string `json:"value"`
		} `json:"permissions"`
	} `json:"result"`
	Message string `json:"message"`
	Type    string `json:"type"`
}

type LoginModal struct {
	Password string `json:"password"`
	Username string `json:"username"`
}

type AuthModal struct {
	Token string `json:"token"`
}

func LoginRoute(c *fiber.Ctx) error {
	info_login := new(LoginModal)
	c.BodyParser(info_login)
	user := info_login.Username
	pass := info_login.Password
	global.Logger.Debug(user, pass)
	if user == config.GetConfig().WebUI.Username && pass == config.GetConfig().WebUI.Password {
		return c.JSON(LoginResultModal{Code: 200, Result: struct {
			Token string "json:\"token\""
		}{Token: config.DESEncode(user + "|" + pass)}, Message: "ok", Type: "success"})
	}
	return c.JSON(LoginErrorModal{Code: -1, Result: nil, Message: "账号或密码错误！", Type: "error"})
}

func AdminInfoRoute(c *fiber.Ctx) error {
	return c.JSON(AdminInfoModal{Code: 200, Message: "ok", Type: "success", Result: struct {
		UserID      string "json:\"userId\""
		Username    string "json:\"username\""
		RealName    string "json:\"realName\""
		Avatar      string "json:\"avatar\""
		Desc        string "json:\"desc\""
		Password    string "json:\"password\""
		Token       string "json:\"token\""
		Permissions []struct {
			Label string "json:\"label\""
			Value string "json:\"value\""
		} "json:\"permissions\""
	}{UserID: "1", Username: config.GetConfig().WebUI.Username, RealName: config.GetConfig().WebUI.Username, Avatar: config.GetConfig().WebUI.Avatar, Desc: "manager", Password: "_", Token: "_", Permissions: []struct {
		Label string "json:\"label\""
		Value string "json:\"value\""
	}{{Label: "主控台", Value: "dashboard_console"}}}})
}

func AuthRoute(c *fiber.Ctx) error {
	t_m := new(AuthModal)
	c.BodyParser(t_m)
	t := t_m.Token
	if config.DESDecode(t) == config.GetConfig().WebUI.Username+"|"+config.GetConfig().WebUI.Password {
		return c.JSON(fiber.Map{"ok": "ok"})
	}
	return c.JSON(fiber.Map{"ok": "no"})
}

func GetPluginsRoute(c *fiber.Ctx) error {
	all := plugin.GetPlugins()
	if all == nil {
		return c.JSON(fiber.Map{"code": 400, "msg": "没有插件"})
	}
	return c.JSON(fiber.Map{"code": 200, "data": all})
}

func GetPluginWritable(c *fiber.Ctx) error {
	name := c.Query("name", "")
	root_path, _ := os.Getwd()
	if !tools.IsFileExist(filepath.Join(root_path, "plugin_data", name+".json")) {
		return c.JSON(fiber.Map{"can": "no"})
	}
	return c.JSON(fiber.Map{"can": "yes"})
}

func SavePlugin(c *fiber.Ctx) error {
	name := c.Query("name", "")
	code := c.Query("code", "")
	root_path, _ := os.Getwd()
	os.WriteFile(filepath.Join(root_path, "plugin_data", name+".json"), []byte(code), 0777)
	return c.JSON(fiber.Map{"msg": "ok"})
}

func GetPluginJsonRoute(c *fiber.Ctx) error {
	name := c.Query("name", "")
	root_path, _ := os.Getwd()
	if tools.IsFileExist(filepath.Join(root_path, "plugin_data", name+".json")) {
		json_b, _ := os.ReadFile(filepath.Join(root_path, "plugin_data", name+".json"))
		return c.SendString(string(json_b))
	}
	return c.SendString(DefaultPluginJson)
}

func WritePluginRoute(c *fiber.Ctx) error {
	name := c.Query("name", "")
	return c.Render("write", fiber.Map{"PluginName": name})
}
